﻿using SharpErp.Data.Common.Entity;
using SharpErp.Data.Common.View;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SharpErp.Data.Service
{
    public partial class SharpErpServiceImpl : ISharpErpService
    {
        public List<ItemFirstClass> GetSysItemFirstClass()
        {
            var list = GetAllKvItemClass<ItemFirstClass>(KVEntity.KEY_ITEM_FIRST_CLASS);
            if (list == null || !list.Any())
            {
                return list;
            }
            return list.Where(p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0).ToList();
        }

        public void AddItemFirstClass(ItemFirstClass item)
        {
            var items = GetAllKvItemClass<ItemFirstClass>(KVEntity.KEY_ITEM_FIRST_CLASS);
            var maxId = 1;
            if (items != null && items.Any()) maxId = (from p in items select p.Id).Max() + 1;
            item.Id = maxId;
            item.CreateTime = item.LastModifyTime = new DateTime();
            if (items == null) return;
            items.Add(item);
            SetAllKvItemClass(items, KVEntity.KEY_ITEM_FIRST_CLASS);
        }



        public void UpdateItemFirstClass(ItemFirstClass item)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                var items = GetAllKvItemClass<ItemFirstClass>(KVEntity.KEY_ITEM_FIRST_CLASS);
                for (var i = 0; i < items.Count(); ++i)
                {
                    if (items[i].Id != item.Id) continue;
                    items[i].LastModifyTime = new DateTime();
                    items[i].Name = item.Name;
                    items[i].Code = item.Code;
                    break;
                }
                SetAllKvItemClass(items, KVEntity.KEY_ITEM_FIRST_CLASS);
            }
        }

        public void DeleteItemFirstClass(int id)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                var items = GetAllKvItemClass<ItemFirstClass>(KVEntity.KEY_ITEM_FIRST_CLASS);
                for (int i = 0; i < items.Count(); ++i)
                {
                    if (items[i].Id == id)
                    {
                        items[i].LastModifyTime = new DateTime();
                        items[i].SystemFlag |= BaseEntity.SYS_FLAG_DELETE;
                        break;
                    }
                }
                SetAllKvItemClass(items, KVEntity.KEY_ITEM_FIRST_CLASS);
            }
        }

        public string GetItemFirstClassCode(int id)
        {
            string maxCode = "0000";
            using (var context = new Common.Context.SharpErpDbContext())
            {
                var items = GetAllKvItemClass<ItemFirstClass>(KVEntity.KEY_ITEM_FIRST_CLASS);
                for (int i = 0; i < items.Count(); ++i)
                {
                    if (items[i].Id == id)
                    {
                        items[i].LastModifyTime = new DateTime();
                        return items[i].Code;
                    }
                    if (items[i].Code.CompareTo(maxCode) > 0) maxCode = items[i].Code;
                }
            }
            return string.Format("{0:D4}", int.Parse(maxCode) + 1);
        }

        public List<ItemSecondClass> GetSysItemSecondClass()
        {
            var list = GetAllKvItemClass<ItemSecondClass>(KVEntity.KEY_ITEM_SECOND_CLASS);
            if (list == null || !list.Any())
            {
                return list;
            }
            return list.Where(p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0).ToList();
        }

        public List<ItemSecondClass> GetSysItemSecondClassByParentId(int parentId)
        {
            var list = GetSysItemSecondClass();
            if (list == null || !list.Any()) return list;
            return list.Where(p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0 & p.ParentId == parentId).ToList();
            ;
        }

        public void AddItemSecondClass(ItemSecondClass item)
        {
            var items = GetAllKvItemClass<ItemSecondClass>(KVEntity.KEY_ITEM_SECOND_CLASS);
            var maxId = 1;
            if (items != null && items.Any()) maxId = (from p in items select p.Id).Max() + 1;
            item.Id = maxId;
            item.CreateTime = item.LastModifyTime = new DateTime();
            if (items == null) return;
            items.Add(item);
            SetAllKvItemClass(items, KVEntity.KEY_ITEM_SECOND_CLASS);
        }

        public void UpdateItemSecondClass(ItemSecondClass item)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                var items = GetAllKvItemClass<ItemSecondClass>(KVEntity.KEY_ITEM_SECOND_CLASS);
                for (var i = 0; i < items.Count(); ++i)
                {
                    if (items[i].Id != item.Id) continue;
                    items[i].LastModifyTime = new DateTime();
                    items[i].Name = item.Name;
                    items[i].ManufactorName = item.ManufactorName;
                    break;
                }
                SetAllKvItemClass(items, KVEntity.KEY_ITEM_SECOND_CLASS);
            }
        }

        public void DeleteItemSecondClass(int id)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                var items = GetAllKvItemClass<ItemSecondClass>(KVEntity.KEY_ITEM_SECOND_CLASS);
                for (int i = 0; i < items.Count(); ++i)
                {
                    if (items[i].Id == id)
                    {
                        items[i].LastModifyTime = new DateTime();
                        items[i].SystemFlag |= BaseEntity.SYS_FLAG_DELETE;
                        break;
                    }
                }
                SetAllKvItemClass(items, KVEntity.KEY_ITEM_SECOND_CLASS);
            }
        }

        public List<ItemClass> GetAllItemClass()
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                return new List<ItemClass>(context.SelectAll(context.ItemClasses, p=>(p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0));
            }
        }

        public void AddItemClass(ItemClass item)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                context.Add(context.ItemClasses, item);
            }
        }

        public void UpdateItemClass(ItemClass item)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                context.Update(context.ItemClasses, item);
            }
        }

        public void DeleteItemClass(int id)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                context.Delete(context.ItemClasses, p => p.Id == id);
            }
        }

        public List<ItemThirdClass> GetSysItemThirdClassByParentId(int parentId)
        {
            var list = GetAllKvItemClass<ItemThirdClass>(KVEntity.KEY_ITEM_THIRD_CLASS);
            if (list == null || !list.Any())
            {
                return list;
            }
            return list.Where(p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0 & p.ParentId == parentId).ToList();
        }

        public void AddItemThirdClass(ItemThirdClass item)
        {
            var items = GetAllKvItemClass<ItemThirdClass>(KVEntity.KEY_ITEM_THIRD_CLASS);
            var maxId = 1;
            if (items != null && items.Any()) maxId = (from p in items select p.Id).Max() + 1;
            item.Id = maxId;
            item.CreateTime = item.LastModifyTime = new DateTime();
            if (items == null) return;
            items.Add(item);
            SetAllKvItemClass(items, KVEntity.KEY_ITEM_THIRD_CLASS);
        }

        public void UpdateItemThirdClass(ItemThirdClass item)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                var items = GetAllKvItemClass<ItemThirdClass>(KVEntity.KEY_ITEM_THIRD_CLASS);
                for (var i = 0; i < items.Count(); ++i)
                {
                    if (items[i].Id != item.Id) continue;
                    items[i].LastModifyTime = new DateTime();
                    items[i].Name = item.Name;
                    items[i].Deduct = item.Deduct;
                    break;
                }
                SetAllKvItemClass(items, KVEntity.KEY_ITEM_THIRD_CLASS);
            };
        }

        public void DeleteItemThirdClass(int id)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                var items = GetAllKvItemClass<ItemThirdClass>(KVEntity.KEY_ITEM_THIRD_CLASS);
                for (int i = 0; i < items.Count(); ++i)
                {
                    if (items[i].Id == id)
                    {
                        items[i].LastModifyTime = new DateTime();
                        items[i].SystemFlag |= BaseEntity.SYS_FLAG_DELETE;
                        break;
                    }
                }
                SetAllKvItemClass(items, KVEntity.KEY_ITEM_THIRD_CLASS);
            }
        }

        public List<ItemForthClass> GetSysItemFourClassByParentId(int parentId)
        {
            var list = GetAllKvItemClass<SharpErp.Data.Common.Entity.ItemForthClass>(KVEntity.KEY_ITEM_FORTH_CLASS);
            if (list == null || !list.Any())
            {
                return list;
            }
            return list.Where(p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0 & p.ParentId == parentId).ToList();
        }

        public void AddItemFourClass(ItemForthClass item)
        {
            var items = GetAllKvItemClass<ItemForthClass>(KVEntity.KEY_ITEM_FORTH_CLASS);
            var maxId = 1;
            if (items != null && items.Any()) maxId = (from p in items select p.Id).Max() + 1;
            item.Id = maxId;
            item.CreateTime = item.LastModifyTime = new DateTime();
            if (items == null) return;
            items.Add(item);
            SetAllKvItemClass(items, KVEntity.KEY_ITEM_FORTH_CLASS);
        }

        public void UpdateItemFourClass(ItemForthClass item)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                var items = GetAllKvItemClass<ItemForthClass>(KVEntity.KEY_ITEM_FORTH_CLASS);
                for (var i = 0; i < items.Count(); ++i)
                {
                    if (items[i].Id != item.Id) continue;
                    items[i].LastModifyTime = new DateTime();
                    items[i].Name = item.Name;
                    break;
                }
                SetAllKvItemClass(items, KVEntity.KEY_ITEM_FORTH_CLASS);
            };
        }

        public void DeleteItemFurClass(int id)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                var items = GetAllKvItemClass<ItemForthClass>(KVEntity.KEY_ITEM_FORTH_CLASS);
                for (int i = 0; i < items.Count(); ++i)
                {
                    if (items[i].Id == id)
                    {
                        items[i].LastModifyTime = new DateTime();
                        items[i].SystemFlag |= BaseEntity.SYS_FLAG_DELETE;
                        break;
                    }
                }
                SetAllKvItemClass(items, KVEntity.KEY_ITEM_FORTH_CLASS);
            }
        }

        public List<ItemFifthClass> GetSysItemFiveClassByParentId(int parentId)
        {
            var list = GetAllKvItemClass<SharpErp.Data.Common.Entity.ItemFifthClass>(KVEntity.KEY_ITEM_FIFTH_CLASS);
            if (list == null || !list.Any())
            {
                return list;
            }
            return list.Where(p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0 & p.ParentId == parentId).ToList();
        }

        public void AddItemFiveClass(ItemFifthClass item)
        {
            var items = GetAllKvItemClass<ItemFifthClass>(KVEntity.KEY_ITEM_FIFTH_CLASS);
            var maxId = 1;
            if (items != null && items.Any()) maxId = (from p in items select p.Id).Max() + 1;
            item.Id = maxId;
            item.CreateTime = item.LastModifyTime = new DateTime();
            if (items == null) return;
            items.Add(item);
            SetAllKvItemClass(items, KVEntity.KEY_ITEM_FIFTH_CLASS);
        }

        public void UpdateItemFiveClass(ItemFifthClass item)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                var items = GetAllKvItemClass<ItemFifthClass>(KVEntity.KEY_ITEM_FIFTH_CLASS);
                for (var i = 0; i < items.Count(); ++i)
                {
                    if (items[i].Id != item.Id) continue;
                    items[i].LastModifyTime = new DateTime();
                    items[i].Name = item.Name;
                    break;
                }
                SetAllKvItemClass(items, KVEntity.KEY_ITEM_FIFTH_CLASS);
            };
        }

        public void DeleteItemFiveClass(int id)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                var items = GetAllKvItemClass<ItemFifthClass>(KVEntity.KEY_ITEM_FIFTH_CLASS);
                for (int i = 0; i < items.Count(); ++i)
                {
                    if (items[i].Id == id)
                    {
                        items[i].LastModifyTime = new DateTime();
                        items[i].SystemFlag |= BaseEntity.SYS_FLAG_DELETE;
                        break;
                    }
                }
                SetAllKvItemClass(items, KVEntity.KEY_ITEM_FIFTH_CLASS);
            }
        }

        public List<ItemSixClass> GetSysItemSixClassByParentId(int parentId)
        {
            var list = GetAllKvItemClass<SharpErp.Data.Common.Entity.ItemSixClass>(KVEntity.KEY_ITEM_SIXTH_CLASS);
            if (list == null || !list.Any())
            {
                return list;
            }
            return list.Where(p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0 & p.ParentId == parentId).ToList();
        }

        public void AddItemSixClass(ItemSixClass item)
        {
            var items = GetAllKvItemClass<ItemSixClass>(KVEntity.KEY_ITEM_SIXTH_CLASS);
            var maxId = 1;
            if (items != null && items.Any()) maxId = (from p in items select p.Id).Max() + 1;
            item.Id = maxId;
            item.CreateTime = item.LastModifyTime = new DateTime();
            if (items == null) return;
            items.Add(item);
            SetAllKvItemClass(items, KVEntity.KEY_ITEM_SIXTH_CLASS);
        }

        public void UpdateItemSixClass(ItemSixClass item)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                var items = GetAllKvItemClass<ItemSixClass>(KVEntity.KEY_ITEM_SIXTH_CLASS);
                for (var i = 0; i < items.Count(); ++i)
                {
                    if (items[i].Id != item.Id) continue;
                    items[i].LastModifyTime = new DateTime();
                    items[i].Name = item.Name;
                    break;
                }
                SetAllKvItemClass(items, KVEntity.KEY_ITEM_SIXTH_CLASS);
            };
        }

        public void DeleteItemSixClass(int id)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                var items = GetAllKvItemClass<ItemSixClass>(KVEntity.KEY_ITEM_SIXTH_CLASS);
                for (int i = 0; i < items.Count(); ++i)
                {
                    if (items[i].Id == id)
                    {
                        items[i].LastModifyTime = new DateTime();
                        items[i].SystemFlag |= BaseEntity.SYS_FLAG_DELETE;
                        break;
                    }
                }
                SetAllKvItemClass(items, KVEntity.KEY_ITEM_SIXTH_CLASS);
            }
        }

        public List<ItemSevenClass> GetSysItemSevenClassByParentId(int parentId)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                return context.SelectAll(context.ItemSevenClasses, p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0 && p.ParentId == parentId);
            }
        }


        public void AddItemSevenClass(ItemSevenClass item)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                context.Add(context.ItemSevenClasses, item);
            }
        }

        public void UpdateItemSevenClass(ItemSevenClass item)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                context.Update(context.ItemSevenClasses, item);
            }
        }

        public void DeleteItemSevenClass(int id)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                context.Delete(context.ItemSevenClasses, p => p.Id == id);
            }
        }


        public List<ItemEightClass> GetSysItemEightClassByParentId(int parentId)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                return context.SelectAll(context.ItemEightClasses,
                    p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0 && p.ParentId == parentId);
            }
        }

        public void AddItemEightClass(ItemEightClass item)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                context.Add(context.ItemEightClasses, item);
            }
        }

        public void UpdateItemEightClass(ItemEightClass item)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                context.Update(context.ItemEightClasses, item);
            }
        }

        public void DeleteItemEightClass(int id)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                context.Delete(context.ItemEightClasses, p => p.Id == id);
            }
        }

        public List<ItemNineClass> GetSysItemNineClassByParentId(int parentId)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                return context.SelectAll(context.ItemNineClasses, p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0 && p.ParentId == parentId);
            }
        }

        public void AddItemNineClass(ItemNineClass item)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                context.Add(context.ItemNineClasses, item);
            }
        }

        public void UpdateItemNineClass(ItemNineClass item)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                context.Update(context.ItemNineClasses, item);
            }
        }


        public void DeleteItemNineClass(int id)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                context.Delete(context.ItemNineClasses, p => p.Id == id);
            }
        }
        public List<ItemSecondClass> GetSysItemSecondClassByParentIds(HashSet<int> parentId)
        {
            var list = GetAllKvItemClass<ItemSecondClass>(KVEntity.KEY_ITEM_SECOND_CLASS);
            if (list == null || !list.Any())
            {
                return list;
            }
            return list.Where(p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0 && parentId.Contains(p.ParentId)).ToList(); ;
        }

        public List<ItemThirdClass> GetSysItemThirdClassByParentIds(HashSet<int> parentId)
        {
            var list = GetAllKvItemClass<ItemThirdClass>(KVEntity.KEY_ITEM_THIRD_CLASS);
            if (list == null || !list.Any())
            {
                return list;
            }
            return list.Where(p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0 && parentId.Contains(p.ParentId)).ToList(); ;
        }

        public List<ItemForthClass> GetSysItemFourClassByParentIds(HashSet<int> parentId)
        {
            var list = GetAllKvItemClass<SharpErp.Data.Common.Entity.ItemForthClass>(KVEntity.KEY_ITEM_FORTH_CLASS);
            if (list == null || !list.Any())
            {
                return list;
            }
            return list.Where(p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0 && parentId.Contains(p.ParentId)).ToList(); ;
        }

        public List<ItemFifthClass> GetSysItemFiveClassByParentIds(HashSet<int> parentId)
        {
            var list = GetAllKvItemClass<SharpErp.Data.Common.Entity.ItemFifthClass>(KVEntity.KEY_ITEM_FIFTH_CLASS);
            if (list == null || !list.Any())
            {
                return list;
            }
            return list.Where(p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0 && parentId.Contains(p.ParentId)).ToList(); ;
        }

        public List<ItemSixClass> GetSysItemSixClassByParentIds(HashSet<int> parentId)
        {
            var list = GetAllKvItemClass<SharpErp.Data.Common.Entity.ItemSixClass>(KVEntity.KEY_ITEM_SIXTH_CLASS);
            if (list == null || !list.Any())
            {
                return list;
            }
            return list.Where(p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0 && parentId.Contains(p.ParentId)).ToList(); ;
        }

        public List<ItemSevenClass> GetSysItemSevenClassByParentIds(HashSet<int> parentId)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                return context.SelectAll(context.ItemSevenClasses,
                    p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0 && parentId.Contains(p.ParentId));
            };
        }

        public List<ItemEightClass> GetSysItemEightClassByParentIds(HashSet<int> parentId)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                return context.SelectAll(context.ItemEightClasses,
                    p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0 && parentId.Contains(p.ParentId));
            };
        }

        public List<ItemNineClass> GetSysItemNineClassByParentIds(HashSet<int> parentId)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                return context.SelectAll(context.ItemNineClasses,
                    p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0 && parentId.Contains(p.ParentId));
            };
        }

        public string GetItemEightClassCode(int id)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                if (id > 0)
                {
                    var item = context.SelectOne(context.ItemEightClasses, p => p.Id == id);
                    if (item != null)
                    {
                        return item.Code;
                    }
                }

                var maxId = context.ItemEightClasses.Max(p => p.Code != null ? p.Code.Substring(p.Code.Length - 7) : p.Code); ;
                if (string.IsNullOrEmpty(maxId))
                {
                    return "S0000001";
                }
                else
                {
                    var number = maxId.Substring(maxId.Length - 7);
                    return string.Format("S{0:D7}", int.Parse(number) + 1);
                }

            }
        }

        public void CloneItemSevenClass(HashSet<int> ids, bool includeChildren)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                var sevenClassList = context.SelectAll(context.ItemEightClasses, p => ids.Contains(p.Id));
                foreach (var itemSevenClass in sevenClassList)
                {
                    var Id = itemSevenClass.Id;
                    itemSevenClass.Id = 0;
                    var newItemSevenClass = context.Add(context.ItemEightClasses, itemSevenClass);
                    if (includeChildren)
                    {
                        var eightClassList = context.SelectAll(context.ItemNineClasses, p => p.ParentId == Id);
                        foreach (var itemEightClass in eightClassList)
                        {
                            itemEightClass.Id = 0;
                            itemEightClass.ParentId = newItemSevenClass.Id;
                            context.Add(context.ItemNineClasses, itemEightClass);
                        }
                    }
                    context.SaveChanges();
                }
            }
        }

        public void CloneItemEightClass(HashSet<int> ids)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                var eightClassList = context.SelectAll(context.ItemNineClasses, p => ids.Contains(p.Id));
                foreach (var itemEightClass in eightClassList)
                {
                    itemEightClass.Id = 0;
                    context.Add(context.ItemNineClasses, itemEightClass);
                }
                context.SaveChanges();

            }
        }

        private QueryGoodsInfoView getQueryGoodsInfoView(ItemEightClass eightClass, ItemNineClass nineClass)
        {
            if (eightClass == null || nineClass == null) return null;
            return new QueryGoodsInfoView() {
                  Name = eightClass.Name,
                  Code = eightClass.Code,
                  Length = eightClass.Length + "*" + eightClass.Width + "*" + eightClass.ParentDesc?.Split(new string[] { "$$" }, StringSplitOptions.RemoveEmptyEntries)[3],
                  Facility = eightClass.Facility,
                  Standard = eightClass.Standard,
                  Unit = eightClass.Unit,
                  ParentDesc = eightClass.ParentDesc,
                  Area = nineClass.Area,
                  AreaPrice = nineClass.AreaPrice,
                  BuyPrice = nineClass.BuyPrice,
                  CheapPrice = nineClass.CheapPrice,
                  Inventory = nineClass.Inventory,
                  MemberPrice = nineClass.MemberPrice,
                  Pcount = eightClass.Pcount,
                  RealMaterialPrice = nineClass.RealMaterialPrice,
                  Requirement = eightClass.Requirement,
                  Width = eightClass.Width,
                  SalePrice = nineClass.SalePrice,
                  ParentId = eightClass.ParentId,
                  EightClassId = eightClass.Id,
                  NineClassId = nineClass.Id,
            };

        }

        private QueryGoodsInfoView getQueryGoodsInfoView(int nineClassId)
        {

            using (var context = new Common.Context.SharpErpDbContext())
            {
                var nineClassItem = context.SelectOne(context.ItemNineClasses, p => p.Id == nineClassId);
                if (nineClassItem == null) return null;
                var eightClassItem = context.SelectOne(context.ItemEightClasses, p => p.Id == nineClassItem.ParentId);
                return getQueryGoodsInfoView(eightClassItem, nineClassItem);
            }
        }

        public List<QueryGoodsInfoView> SearchGoodsInfo(String keyWords)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                var items = keyWords.Split(new char[] { ' ', }, StringSplitOptions.RemoveEmptyEntries);
                var queryResult = new List<QueryGoodsInfoView>();
                List< ItemEightClass > sevenClassList = null;
                foreach(var item in items)
                {
                    if(sevenClassList == null || sevenClassList.Count == 0)
                    {
                        sevenClassList = new List<ItemEightClass>(context.SelectAll(context.ItemEightClasses, p => p.Name != null && p.Name.Contains(item) ||
                        p.Code != null && p.Code.Contains(item) ||
                        p.ParentDesc != null && p.ParentDesc.Contains(item)));
                    }
                    else
                    {
                        sevenClassList = sevenClassList.Where(p => p.Name != null && p.Name.Contains(item) ||
                        p.Code != null && p.Code.Contains(item) ||
                        p.ParentDesc != null && p.ParentDesc.Contains(item)).ToList();
                    }
                }
                foreach(var item in sevenClassList)
                {
                    var eightClassList = this.GetSysItemNineClassByParentId(item.Id);
                    foreach(var eightClassItem in eightClassList)
                    {
                        var newGoodsInfoViewItem = getQueryGoodsInfoView(item, eightClassItem);
                        if(newGoodsInfoViewItem != null) queryResult.Add(newGoodsInfoViewItem);
                    }
                }
                return queryResult;
            }
        }

        public void AddGoodsMaterialClass(GoodsMaterialClass entity)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                context.Add(context.GoodsMaterialClasses, entity);
            }
        }

        public void UpdateGoodsMaterialClass(GoodsMaterialClass entity)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                context.Update(context.GoodsMaterialClasses, entity);
            }
        }

        public void DeleteGoodsMaterialClass(int id)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                context.Delete(context.GoodsMaterialClasses, p => p.Id == id);
            }
        }

        public List<GoodsMaterialClass> GetGoodsMaterialViewByEightClassId(int bindType, int eightClassId)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                return new List<GoodsMaterialClass>(context.SelectAll(context.GoodsMaterialClasses, p => p.BindType == bindType && p.ItemEightClassId == eightClassId && (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0));
            }
        }

    }

}
